<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-20
 * Time: 下午4:17
 */
 
class BaoBeiInfoController extends Controller{

    public function index(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);
        $baobei_name = $this->input()->post('baobei_name');
        $baobei_source = $this->input()->post('source');
        $baobei_type = $this->input()->post('type');
        $filter = array();

        if($baobei_name){
            $filter['baobei_name'] = $baobei_name;
        }

        if($baobei_type){
            $filter['baobei_type_id'] = $baobei_type;
        }

        if($baobei_source){
            $filter['baobei_source_id'] = $baobei_source;
        }

        $list = Model::getInstance('baobeiinfo')->getBaoBeiInfoList($curr_page,$per_page,$filter);
        $type_arr = Model::getInstance('baobeitype')->getAllTypeInfo();

        $type_list = array();
        foreach($type_arr as $v){
            $type_list[$v['type_id']] = $v['type_name'];
        }

        $source_arr = Model::getInstance('baobeisource')->getAllSourceList();

        $source_list = array();
        foreach($source_arr as $v){
            $source_list[$v['id']] = $v['zh_name'];
        }

        $data = array(
            'list'=>$list,
            'type_list' => $type_list,
            'source_list'=>$source_list
        );
        Template::show('baobei/baobeiinfo.php',$data);

    }

    public function add(){

        Template::show('baobei/add_baobeiinfo.php');
    }

    public function del(){

        $baobei_id = $this->input()->get('baobei_id');

        $ret = Model::getInstance('baobeiinfo')->delBaoBeiInfo($baobei_id);

        if($ret){

            $data = array(
                "statusCode"=>"200",
                "navTabId"=>"baobeiinfo",
            );
            echo Response::JSON($data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON($data);
        }
    }

    public function edit(){

        $type_id = (int)$this->input()->get('type_id');

        $type_info = Model::getInstance('baobeitype')->get($type_id);

        if($type_info['parent_type_id']){

            $parent_type_info = Model::getInstance('baobeitype')->get($type_info['parent_type_id']);
            
        }else{

            $parent_type_info = array(
                'type_id'=>0,
                'type_name'=>'发现宝贝'
            );
        }

        $data = array(
            'type_info'=>$type_info,
            'parent_type_info'=>$parent_type_info
        );

        Template::show('baobei/edit_baobeitype.php',$data);
    }

}